package cn.edu.scau.dormitory_management_system.module.examine.controller;

import cn.edu.scau.dormitory_management_system.constant.Consts;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.CheckinApplicationEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.VO.CheckinApplicationVO;
import cn.edu.scau.dormitory_management_system.module.examine.service.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ：Vizzk
 * @description：TODO
 * @date ：2021/8/6 10:55
 */
@RestController
@RequestMapping("/admin/checkin")
public class CheckinApplicationController {

    @Autowired
    CheckinApplyService checkinApplyService;

    @Autowired
    CheckinApplicationService checkinApplicationService;

    @PostMapping("/insertApplication")
    public Object insertApplication(HttpServletRequest request, @RequestBody CheckinApplicationEntity applicationEntity){
        //创建流程
        JSONObject jsonObject = new JSONObject();
        UserEntity userEntity = (UserEntity) request.getAttribute("admin");
        applicationEntity.setUserId(userEntity.getUserId());
        applicationEntity.setCommitTime(new Date());
        try {
            checkinApplyService.applyCheckin(applicationEntity);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "提交成功");
        } catch (Exception e){
            e.printStackTrace();
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }

    @GetMapping("/listApplicationByUser")
    public Object listApplicationByUser(HttpServletRequest request, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize){
        JSONObject jsonObject = new JSONObject();
        UserEntity user = (UserEntity) request.getAttribute("admin");

        try {
            IPage<CheckinApplicationVO> page = checkinApplicationService.listCheckinApplicationsByUserId(user.getUserId(),pageNum,pageSize);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "查询成功");
            jsonObject.put("pages",page);
        } catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "查询失败");
        }
        return jsonObject;
    }

    @GetMapping("/listApplications")
    public Object listApplications(HttpServletRequest request,Integer deptId, Integer status, String name, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize){
        JSONObject jsonObject = new JSONObject();
        try {
            IPage<CheckinApplicationVO> page = checkinApplicationService.listCheckinApplications(status,deptId,name,pageNum,pageSize);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "查询成功");
            jsonObject.put("pages",page);
        } catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "查询失败");
        }
        return jsonObject;
    }

    /**根据学院管理员身份获取申请表*/
    @GetMapping("/listApplicationByDeptAdmin")
    public Object listApplicationsByDeptAdmin(HttpServletRequest request, Integer status, String name, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize){
        JSONObject jsonObject = new JSONObject();
        UserEntity user = (UserEntity) request.getAttribute("admin");
        try {
            IPage<CheckinApplicationVO> page = checkinApplicationService.listCheckinApplications(status, user.getDeptId(), name,pageNum, pageSize);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "查询成功");
            jsonObject.put("page",page);
        } catch (Exception e){
            e.printStackTrace();
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "查询失败");
        }
        return jsonObject;
    }

    /**根据申请表id撤销申请表申请表*/
    @GetMapping("/retreatByCheckinId")
    public Object retreatByCheckinId(HttpServletRequest request, Integer id){
        JSONObject jsonObject = new JSONObject();
        try {
            checkinApplyService.retreatApplication(id);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "撤回成功");
        } catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "撤回失败");
        }
        return jsonObject;
    }
    /** 逻辑删除申请表 **/
    @GetMapping("/deleteByCheckinId")
    public Object deleteByCheckinId(HttpServletRequest request, Integer id){
        JSONObject jsonObject = new JSONObject();
        try {
            checkinApplicationService.removeById(id);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "删除成功");
        } catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "删除失败");
        }
        return jsonObject;
    }
}
